var isExistDiv = document.getElementById("koe_xhr_text");
if(isExistDiv){
  document.body.removeChild(document.getElementById("koe_xhr_text"));
}
var koe_xhr = null;
if (window.XMLHttpRequest) koe_xhr = new XMLHttpRequest();
if (window.ActiveXObject) koe_xhr = new ActiveXObject('Microsoft.XMLHTTP');


function addDom() {
  var guodiv = document.createElement("div");
  guodiv.setAttribute("id", "koe_xhr_text");
  guodiv.interHTML = "选中要翻译的英文";
  document.body.insertBefore(guodiv, document.body.firstChild)
}
function createStyle() {
  return "#koe_xhr_text{position:fixed;background:#333333eb;left:50%;color:#fff;font-size:50px;padding:20px 30px;z-index:100000;top:50%;left:50%;-webkit-transform:translate(-50%,-50%);-moz-transform:translate(-50%,-50%);transform:translate(-50%,-50%);}"
}
function addStyle() {
  var style = document.createElement("style");
  style.type = "text/css";
  style.innerHTML = createStyle();
  window.document.head.appendChild(style)
}
addStyle();
addDom();


if (koe_xhr !== null) {
  koe_xhr.onreadystatechange = function() {
    if (koe_xhr.readyState === 4 && koe_xhr.status === 200) {
      var json = JSON.parse(koe_xhr.responseText);
      var koe_xhr_text = document.querySelector('#koe_xhr_text');
      if (koe_xhr_text === null) {
        koe_xhr_text = document.createElement('p');
        koe_xhr_text.setAttribute('id', 'koe_xhr_text');
        koe_xhr_text.setAttribute('style', 'position: fixed; top: 0; left: 0; padding: 3px; background-color: #000; color: #FFF; font-size: 26px;');
        document.body.appendChild(koe_xhr_text)
      }
      koe_xhr_text.innerText = json.sentences[0].trans
    }
  };
  koe_xhr.open('GET', 'https://bili.imba97.cn/translate.php?kw=' + window.getSelection().toString(), true);
  koe_xhr.send(null)
}
setTimeout(function () {
  koe_xhr_text.style.display = "none";
}, 2000);
